package com.yaowk.system.controller;

import com.feizhou.swagger.annotation.Api;
import com.feizhou.swagger.annotation.ApiOperation;
import com.feizhou.swagger.annotation.Param;
import com.feizhou.swagger.annotation.Params;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yaowk.common.controller.BaseController;
import com.yaowk.system.model.Menu;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.List;

/**
 * @authc yaowk
 * 2017/7/13
 */
@RequiresAuthentication
@RequiresPermissions("system:menu")
@Api(url = "/system/menu", tag = "Menu", description = "菜单操作接口，需权限system:menu")
public class MenuController extends BaseController {

    @ApiOperation(url = "/list", description = "获取所有菜单按钮")
    public void list() {
        List<Menu> menuList = Menu.dao.find(Kv.create());
        menuList = Menu.dao.parseMenu(menuList);
        renderJson(menuList);
    }

    @ApiOperation(url = "/{menuId}", description = "获取菜单详情")
    public void index() {
        Integer id = getParaToInt();
        Menu menu = Menu.dao.findById(id);
        List<Menu> menuList = Menu.dao.find(Kv.by("parentId = ", id));
        menu.put("children", menuList);
        renderJson(menu);
    }

    @Before(Tx.class)
    @RequiresPermissions("system:menu:add")
    @ApiOperation(url = "/add", description = "添加菜单，需拥有system:menu:add", httpMethod = "post")
    @Params({
            @Param(name = "name", description = "菜单名称", required = true),
            @Param(name = "url", description = "请求路径"),
            @Param(name = "description", description = "描述"),
            @Param(name = "permission", description = "拥有权限"),
            @Param(name = "parentId", description = "上级菜单", dataType = "Integer", defaultValue = "0"),
            @Param(name = "sort", description = "排序", dataType = "Integer"),
            @Param(name = "type", description = "类型", dataType = "Integer")
    })
    public void add() {
        Menu menu = getBean(Menu.class, "");
        menu.save();
        renderJson(menu);
    }

    @Before(Tx.class)
    @RequiresPermissions("system:menu:edit")
    @ApiOperation(url = "/edit/{menuId}", description = "修改菜单,需拥有system:menu:edit", httpMethod = "post")
    @Params({
            @Param(name = "name", description = "菜单名称", required = true),
            @Param(name = "url", description = "请求路径"),
            @Param(name = "description", description = "描述"),
            @Param(name = "permission", description = "拥有权限"),
            @Param(name = "parentId", description = "上级菜单", dataType = "Integer", defaultValue = "0"),
            @Param(name = "sort", description = "排序", dataType = "Integer"),
            @Param(name = "type", description = "类型", dataType = "Integer")
    })
    public void edit() {
        Menu menu = getBean(Menu.class, "", true);
        menu.setId(getParaToInt());
        menu.update();
        renderJson(menu);
    }

    @Before(Tx.class)
    @RequiresPermissions("system:menu:delete")
    @ApiOperation(url = "/delete/{menuId}", description = "删除菜单,需拥有system:menu:delete")
    public void delete() {
        Integer id = getParaToInt();
        new Menu().setId(id).delete();
        renderSuccess();
    }
}
